namespace RushMail.Requests;

/// <summary>
/// 域名相关接口
/// </summary>
public class DomainEmail
{
    private readonly IRushMailClient _client;

    public DomainEmail(IRushMailClient client)
    {
        _client = client;
    }

    /// <summary>
    /// 域名邮件账号列表
    /// http://edmapi.rushmail.com/api.php?module=domain-email&action=list
    /// </summary>
    /// <returns></returns>
    public async Task<RushMailResponse<DomainEmailListResponse>> List(int domainId, int page = 1) =>
        (await _client.PostAsync(new RushMailRequest("domain-email", "list")
        {
            Body = new Dictionary<string, string>() { {"domain_id", domainId.ToString()}, {"page", page.ToString()}}
        })).ToGeneric<DomainEmailListResponse>();
    
    
    /// <summary>
    /// 添加域名邮件账号
    /// http://edmapi.rushmail.com/api.php?module=domain-email&action=add
    /// </summary>
    /// <returns></returns>
    public async Task<RushMailResponse<int>> Add(DomainEmailAdd body) =>
        (await _client.PostAsync(new RushMailRequest("domain-email", "add")
        {
            Body = body.ToDictionary()
        }))
        .ToGeneric<int>();
    
    /// <summary>
    /// 修改邮件账号
    /// http://edmapi.rushmail.com/api.php?module=domain-email&action=edit
    /// </summary>
    /// <returns></returns>
    public Task<RushMailResponse> Edit(DomainEmailEdit body) =>
        _client.PostAsync(new RushMailRequest("domain-email", "edit")
        {
            Body = body.ToDictionary()
        });

    /// <summary>
    /// 删除邮件账号
    /// http://edmapi.rushmail.com/api.php?module=domain-email&action=delete
    /// </summary>
    /// <returns></returns>
    public Task<RushMailResponse> Delete(int emailId) =>
        _client.PostAsync(new RushMailRequest("domain-email", "delete")
        {
            Body = new Dictionary<string, string>() { {"email_id", emailId.ToString()} }
        });

}


public class DomainEmailAdd
{
    public int DomainId { get; set; }
    public string Account { get; set; }
    public string Name { get; set; }
    public string Password { get; set; }
    /// <summary>
    /// -1 禁用 1 正常
    /// </summary>
    public int Status { get; set; }

    public virtual Dictionary<string, string> ToDictionary() => new Dictionary<string, string>()
    {
        { "domain_id", DomainId.ToString() },
        { "account", Account },
        { "name", Name },
        { "password", Password },
        { "status", Status.ToString() },
    };
}

public class DomainEmailEdit
{
    public int EmailId { get; set; }
    public string Name { get; set; }
    public string Password { get; set; } = string.Empty;
    /// <summary>
    /// -1 禁用 1 正常
    /// </summary>
    public int Status { get; set; }

    public virtual Dictionary<string, string> ToDictionary() => new Dictionary<string, string>()
    {
        { "email_id", EmailId.ToString() },
        { "name", Name },
        { "password", Password },
        { "status", Status.ToString() },
    };
}

public class DomainEmailListResponse
{
    [JsonProperty("domain_email_list")]public DomainEmailListPage DomainEmailList { get; set; }
}

public class DomainEmailListPage
{
    public List<DomainEmailItem> List { get; set; }
    public Paginate Page { get; set; }
}

public class DomainEmailItem
{
    [JsonProperty("email_id")]
    public int EmailId { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    /// <summary>
    /// 任务状态 -1 禁用 1 正常
    /// </summary>
    public int Status { get; set; }
}
